/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.ptr.ptrsystem.servlet;
import com.ptr.controladores.conexionsqlserver;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.net.URL;

/**
 *
 * @author pavel
 */
@WebServlet(name = "MantenimientoListas", urlPatterns = {"/MantenimientoListas"})
public class MantenimientoListasServlet extends HttpServlet {

    public MantenimientoListasServlet() {
    }
    String CUI_PERS;
    String NroActa;
    String Dato1,Dato2,Dato3, Dato4;
    String casos;
    conexionsqlserver base=new conexionsqlserver();
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        CUI_PERS=request.getParameter("cui");
        NroActa=request.getParameter("NActa");
        Dato1=request.getParameter("PApe");
        Dato2=request.getParameter("Sape");
        Dato3=request.getParameter("Pnom");
        String boton=request.getParameter("btn1");
        casos=request.getParameter("Tbus");
        int x=Integer.parseInt(casos);
        if("BuscaListaNac".equals(boton))
        {
            switch (x){
                case 0:
                    request.setAttribute("msj", "1");
                    request.getRequestDispatcher("/jsp/BNacimiento.jsp").forward(request, response);
                    break;
                case 1:BuscarCUI(request, response);break;
                case 2:BuscarActa(request, response);break;
                case 3:DatosPersonales(request, response);break;
                case 4:UbigeoPersona(request, response);break;
            }
        }
        
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP
     * <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Handles the HTTP
     * <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

    private void BuscarCUI(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        out.println("<html>");
        out.println("<title>Sistema de Registros Civiles</title>");
        out.println("<head>");
        out.println("<meta http-equiv='Content-Type' content='text/html; charset=UTF-8'>");
        out.println("<link rel='stylesheet' type='text/css' href='/RegCivil/css/estilos.css\'>");
        out.println("</head>");
        out.println("<body>");
        out.println("<form action='ConsultasActasImagen' method='POST'>");
        out.println("<table border=1 style='border-collapse:collapse; background-color: #f6f4f0'><tr><td>");
        out.println("<center><Label style='font-size:2em'/><img src='/RegCivil/imagenes/kuser.png'>RESULTADO DE CONSULTA POR NUMERO DE CUI</center>");
        out.println("<table border=1 cellpadding='2' cellspacing='4' style='background-color: #f0f5f9;border-collapse:collapse'>");
        out.println("<tr>");
        out.println("<td style='width:20px'>Val</td>");
        out.println("<td style='width:40px'>ITEM</td>");
        out.println("<td style='width:60px'>CUI</td>");
        out.println("<td style='width:70px'>NRO ACTA</td>");
        out.println("<td style='width:70px'>NRO LIBRO</td>");
        out.println("<td style='width:130px'>PRIMER APELLIDO</td>");
        out.println("<td style='width:130px'>SEGUNDO APELLIDO</td>");
        out.println("<td style='width:130px'>APE. CASADA</td>");
        out.println("<td style='width:120px'>PRENOMBRE(S)</td>");
        out.println("<td style='width:70px;border:1px solid;background-color: #ffeae3'>VER DATOS</td>");
        out.println("</tr>");
        int a=0;
         try
          {              
             base.conectar();
             Connection conn=base.getconection();
             Statement st = conn.createStatement();
             String query="select n.CUI_PERS, n.NRO_ACTA_PERS, ";
             query=query+" n.NRO_ACTA_PERS, p.PRIM_APE_PERS,";
             query=query+" p.SEG_APE_PERS, p.APE_CASADA,";
             query=query+" p.PRE_NOM_PERS, n.RUTA_ACTA_NAC";
             query=query+" from TB_PERSONA p, TB_NACIMIENTO n";
             query=query+" where p.COD_PERSONA=n.COD_PERSONA";
             query= query+" and n.CUI_PERS like'%"+CUI_PERS +"%'";
             ResultSet rs = st.executeQuery(query);
             while (rs.next())
             {
                 a=a+1;
                 out.println("<tr><td style='width:20px;border: 1px solid #fdfbf5'><INPUT TYPE =radio name='ChkDatos' value='"+rs.getString(2)+"' onclick=confirm('"+ rs.getString(2) +"')><img src='/RegCivil/imagenes/chek.png' width='15px' heigth='15px'></td>");
                 out.println("<td style='width:40px;border: 1px solid #fdfbf5;font-weight:bold'>"+ a +"</td>");
                 out.println("<td style='width:60px;border: 1px solid #fdfbf5;font-weight:bold'>"+rs.getString(1)+"</td>");
                 out.println("<td style='width:70px;border: 1px solid #fdfbf5;font-weight:bold'>"+rs.getString(2)+"</td>");
                 out.println("<td style='width:70px;border: 1px solid #fdfbf5;font-weight:bold'>"+rs.getString(3)+"</td>");
                 out.println("<td style='width:130px;border: 1px solid #fdfbf5;font-weight:bold'>"+rs.getString(4)+"</td>");
                 out.println("<td style='width:130px;border: 1px solid #fdfbf5;font-weight:bold'>"+rs.getString(5)+"</td>");
                 out.println("<td style='width:130px;border: 1px solid #fdfbf5;font-weight:bold'>"+rs.getString(6)+"</td>");
                 out.println("<td style='width:130px;border: 1px solid #fdfbf5;font-weight:bold'>"+rs.getString(7)+"</td>" );
                 //out.println("<td style='width:70px;border: 1px solid #f8c1c1;font-weight:bold'><a href='" +rs.getString(8)+ "' target=_blank><img src='/RegCivil/imagenes/persona.png' style='width:40px; heigth:40px'></a></td>");
                 out.println("<td style='width:70px;border: 1px solid #f8c1c1;font-weight:bold'>"
                         + "<button type='submit' name='btn1' value='BDatos' style='border:1px solid'><img src='/RegCivil/imagenes/persona.png' style='width:40px; heigth:40px'/></button></a></td>");
                 out.println("</tr>");
             }
             rs.close();
             st.close();
             base.cerrarBD();
          }
          catch (Exception e)
          {
             e.printStackTrace();
          }
    out.println("<tr>");
    out.println("<td colspan=10 align=right>");
    out.println("<button type=button onclick=JavaScript:location.href='/RegCivil/jsp/BNacimiento.jsp'; style='width:10.0em; heigth:8em; background-color: #eeeeee;border: 1px solid #9cafff;border-radius: 7px 7px 7px 7px;'><img src='/RegCivil/imagenes/return.png'>Retornar</button>");
    out.println("</td>");
    out.println("</tr>");
    out.println("</tr>");
    out.println("</table>");
    out.println("</td></td>");
    out.println("</form>");
    out.println("</body>");
    out.println("</html>");
    }

    private void BuscarActa(HttpServletRequest request, HttpServletResponse response) 
    throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        out.println("<html>");
        out.println("<title>Sistema de Registros Civiles</title>");
        out.println("<head>");
            out.println("<link rel='stylesheet' type='text/css' href='/RegCivil/css/estilos.css\'>");
        out.println("</head>");
        out.println("<body>");
        out.println("<form action='ConsultasActasImagen' method='POST'>");
        out.println("<table border=1 style='border-collapse:collapse; background-color: #f6f4f0'><tr><td>");
        out.println("<center><Label style='font-size:2em'/><img src='/RegCivil/imagenes/kuser.png'>RESULTADO DE CONSULTA POR NUMERO DE ACTA</center>");
        out.println("<table border=1 cellpadding='2' cellspacing='4' style='background-color: #f0f5f9;border-collapse:collapse'>");
        out.println("<tr>");
        out.println("<td style='width:20px'>Val</td>");
        out.println("<td style='width:40px'>ITEM</td>");
        out.println("<td style='width:60px'>CUI</td>");
        out.println("<td style='width:70px'>NRO ACTA</td>");
        out.println("<td style='width:70px'>NRO LIBRO</td>");
        out.println("<td style='width:130px'>PRIMER APELLIDO</td>");
        out.println("<td style='width:130px'>SEGUNDO APELLIDO</td>");
        out.println("<td style='width:130px'>APE. CASADA</td>");
        out.println("<td style='width:120px'>PRENOMBRE(S)</td>");
        out.println("<td style='width:70px;border:1px solid;background-color: #ffeae3'>VER DATOS</td>");
        out.println("</tr>");
        int a=0;
         try
          {              
             base.conectar();
             Connection conn=base.getconection();
             Statement st = conn.createStatement();
             
             String query1="select n.CUI_PERS, n.NRO_ACTA_PERS, ";
             query1=query1+" n.NRO_ACTA_PERS, p.PRIM_APE_PERS,";
             query1=query1+" p.SEG_APE_PERS, p.APE_CASADA,";
             query1=query1+" p.PRE_NOM_PERS, n.RUTA_ACTA_NAC";
             query1=query1+" from TB_PERSONA p, TB_NACIMIENTO n";
             query1=query1+" where p.COD_PERSONA=n.COD_PERSONA";
             query1= query1+" and n.NRO_ACTA_PERS like'%"+NroActa +"%'";
             ResultSet rs1 = st.executeQuery(query1);
             while (rs1.next())
             {
                 a=a+1;
                 out.println("<tr><td style='width:20px;border: 1px solid #fdfbf5'><INPUT TYPE =radio name='ChkDatos' value='"+rs1.getString(2)+"' onclick=confirm('"+ rs1.getString(2) +"')><img src='/RegCivil/imagenes/chek.png' width='15px' heigth='15px'></td>");
                 out.println("<td style='width:40px;border: 1px solid #fdfbf5;font-weight:bold'>"+ a +"</td>");
                 out.println("<td style='width:60px;border: 1px solid #fdfbf5;font-weight:bold'>"+rs1.getString(1)+"</td>");
                 out.println("<td style='width:70px;border: 1px solid #fdfbf5;font-weight:bold'>"+rs1.getString(2)+"</td>");
                 out.println("<td style='width:70px;border: 1px solid #fdfbf5;font-weight:bold'>"+rs1.getString(3)+"</td>");
                 out.println("<td style='width:130px;border: 1px solid #fdfbf5;font-weight:bold'>"+rs1.getString(4)+"</td>");
                 out.println("<td style='width:130px;border: 1px solid #fdfbf5;font-weight:bold'>"+rs1.getString(5)+"</td>");
                 out.println("<td style='width:130px;border: 1px solid #fdfbf5;font-weight:bold'>"+rs1.getString(6)+"</td>");
                 out.println("<td style='width:130px;border: 1px solid #fdfbf5;font-weight:bold'>"+rs1.getString(7)+"</td>" );
                 //out.println("<td style='width:70px;border: 1px solid #f8c1c1;font-weight:bold'><a href='" +rs1.getString(8)+ "' target=_blank><img src='/RegCivil/imagenes/persona.png' style='width:40px; heigth:40px' alt='Ver datos detallados por persona'></a></td>");
                 out.println("<td style='width:70px;border: 1px solid #f8c1c1;font-weight:bold'>"
                         + "<button type='submit' name='btn1' value='BDatos' style='border:1px solid'><img src='/RegCivil/imagenes/persona.png' style='width:40px; heigth:40px'/></button></a></td>");
                 out.println("</tr>");
             }
             rs1.close();
             st.close();
             base.cerrarBD();
          }
          catch (Exception e)
          {
             e.printStackTrace();
          }
    out.println("<tr>");
    out.println("<td colspan=10 align=right>");
    out.println("<button type=button onclick=JavaScript:location.href='/RegCivil/jsp/BNacimiento.jsp'; style='width:10.0em; heigth:8em; background-color: #eeeeee;border: 1px solid #9cafff;border-radius: 7px 7px 7px 7px;'><img src='/RegCivil/imagenes/return.png'>Retornar</button>");
    out.println("</td>");
    out.println("</tr>");
    out.println("</table>");
    out.println("</td></td>");
    out.println("</form>");
    out.println("</body>");
    out.println("</html>");
    }

    private void DatosPersonales(HttpServletRequest request, HttpServletResponse response)
     throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        //out.println("<html>");
        out.println("<!DOCTYPE html>");
        out.println("<html lang='es'>");
        out.println("<title>Sistema de Registros Civiles</title>");
        out.println("<head>");
        out.println("<link rel='stylesheet' type='text/css' href='/RegCivil/css/estilos.css'>");
        out.println("</head>");
        //out.println("<body>");
        out.println("<form action='ConsultasActasImagen' method='POST'>");
        out.println("<table border=1 style='border-collapse:collapse; background-color: #f6f4f0'><tr><td>");
        out.println("<center><Label style='font-size:2em'/><img src='/RegCivil/imagenes/kuser.png'>RESULTADO DE CONSULTA POR DATOS PERSONALES</center>");
        out.println("<table border=1 cellpadding='2' cellspacing='4' style='background-color: #f0f5f9;border-collapse:collapse'>");
        out.println("<tr>");
        out.println("<td style='width:20px;border:1px solid'>Val</td>");
        out.println("<td style='width:40px;border:1px solid'>ITEM</td>");
        out.println("<td style='width:60px;border:1px solid'>CUI</td>");
        out.println("<td style='width:70px;border:1px solid'>NRO ACTA</td>");
        out.println("<td style='width:70px;border:1px solid'>NRO LIBRO</td>");
        out.println("<td style='width:130px;border:1px solid'>PRIMER APELLIDO</td>");
        out.println("<td style='width:130px;border:1px solid'>SEGUNDO APELLIDO</td>");
        out.println("<td style='width:130px;border:1px solid'>APE. CASADA</td>");
        out.println("<td style='width:120px;border:1px solid'>PRENOMBRE(S)</td>");
        out.println("<td style='width:70px;border:1px solid;background-color: #ffeae3'>VER DATOS</td>");
        out.println("</tr>");
        int a=0;
         try
          {              
             base.conectar();
             Connection conn=base.getconection();
             Statement st = conn.createStatement();
             String query2="select n.CUI_PERS, n.NRO_ACTA_PERS,";
             query2=query2+" n.NRO_LIBRO, p.PRIM_APE_PERS,";
             query2=query2+" p.SEG_APE_PERS, p.APE_CASADA,";
             query2=query2+" p.PRE_NOM_PERS, n.RUTA_ACTA_NAC";
             query2=query2+" from TB_PERSONA p, TB_NACIMIENTO n";
             query2=query2+" where p.COD_PERSONA=n.COD_PERSONA";
             query2= query2+" and p.PRIM_APE_PERS like'%"+Dato1+"%' AND p.SEG_APE_PERS like '%"+Dato2+"%' AND p.PRE_NOM_PERS like '%"+Dato3+"%'";
             ResultSet rs2 = st.executeQuery(query2);
             while (rs2.next())
             {
                 a=a+1;
                 out.println("<tr><td style='width:20px;;border: 1px solid #fdfbf5'><INPUT TYPE=radio name=ChkDatos value='"+rs2.getString(2)+"' onclick=confirm('"+ rs2.getString(2) +"')><img src='/RegCivil/imagenes/chek.png' width='15px' heigth='15px'></td>");
                 out.println("<td style='width:40px;border: 1px solid #fdfbf5;font-weight:bold'>"+ a +"</td>");
                 out.println("<td style='width:60px;border: 1px solid #fdfbf5;font-weight:bold'>"+rs2.getString(1)+"</td>");
                 out.println("<td style='width:70px;border: 1px solid #fdfbf5;font-weight:bold'>"+rs2.getString(2)+"</td>");
                 out.println("<td style='width:70px;border: 1px solid #fdfbf5;font-weight:bold'>"+rs2.getString(3)+"</td>");
                 out.println("<td style='width:130px;border: 1px solid #fdfbf5;font-weight:bold'>"+rs2.getString(4)+"</td>");
                 out.println("<td style='width:130px;border: 1px solid #fdfbf5;font-weight:bold'>"+rs2.getString(5)+"</td>");
                 out.println("<td style='width:130px;border: 1px solid #fdfbf5;font-weight:bold'>"+rs2.getString(6)+"</td>");
                 out.println("<td style='width:130px;border: 1px solid #fdfbf5;font-weight:bold'>"+rs2.getString(7)+"</td>" );
                 out.println("<td style='width:70px;border: 1px solid #f8c1c1;font-weight:bold'>"
                         + "<button type='submit' name='btn1' value='BDatos' style='border:1px solid'><img src='/RegCivil/imagenes/persona.png' style='width:40px; heigth:40px'/></button></a></td>");
                 out.println("</tr>");
             }
             rs2.close();
             st.close();
             base.cerrarBD();
          }
          catch (Exception e)
          {
             e.printStackTrace();
          }
    out.println("<tr>");
    out.println("<td colspan=10 align=right>");
    out.println("<button type=button onclick=JavaScript:location.href='/RegCivil/jsp/BNacimiento.jsp'; style='width:10.0em; heigth:8em; background-color: #eeeeee;border: 1px solid #9cafff;border-radius: 7px 7px 7px 7px;'><img src='/RegCivil/imagenes/return.png'>Retornar</button>");
    out.println("</td>");
    out.println("</tr>");
    out.println("</table>");
    out.println("</td></td>");
    out.println("</form>");
    out.println("</html>");
    }

    private void UbigeoPersona(HttpServletRequest request, HttpServletResponse response) 
    throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        out.println("<table>");
        out.println("<tr>");
            out.println("<td>");
                        out.println("<div>");
                            out.println("<fieldset style='border-radius: 10px 10px 10px 10px; background-color: #fff5f6;border:1px solid #f57269'>");
                                out.println("<legend style='border-radius: 10px 10px 10px 10px; background-color: #fff5f6;border:1px solid #f57269'><img src='/RegCivil/imagenes/exclamation.png'> </legend>");
                                   out.println("Este modulo de consulta no esta vigente, realice por los anteriore stipos de busqueda");
                                    out.println("<br> Gracias....<br>");
                                    out.println("<button type=button onclick=JavaScript:location.href='/RegCivil/jsp/BNacimiento.jsp'; style='width:10.0em; heigth:8em; background-color: #eeeeee;border: 1px solid #9cafff;border-radius: 7px 7px 7px 7px;'><img src='/RegCivil/imagenes/return.png'>Retornar</button>");
                                        out.println("</fieldset>");
                                        out.println("</div>");
                              out.println("<td>");
                            out.println("</tr>");
        out.println("</table>");
    }
}